package com.rokyinfo.pdm.controller.v1;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.rokyinfo.pdm.controller.BasicPath;
import com.rokyinfo.pdm.entity.DDevice;
import com.rokyinfo.pdm.service.IDDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 设备表 前端控制器
 * </p>
 *
 * @author YuanZhiJian
 * @since 2017-01-22
 */
@Api(value = "/devices", tags = "Devices", description = "设备")
@Controller
@RequestMapping(BasicPath.V1 + "/devices")
public class DDeviceController {
    @Autowired
    private IDDeviceService mIDDeviceService;

    @ApiOperation(value = "获取设备列表", notes = "")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity<Page<DDevice>> getDDeviceList(@RequestParam(value = "offset", required = false, defaultValue = "1") Integer offset, @RequestParam(value = "limit", required = false, defaultValue = "20") Integer limit) throws Exception {
        // 还可以通过@RequestParam从页面中传递参数来进行查询条件或者翻页信息的传递
        EntityWrapper<DDevice> wrapper = new EntityWrapper<DDevice>();
        return ResponseEntity.ok(mIDDeviceService.selectPage(new Page<DDevice>(offset, limit), wrapper));
    }

    @ApiOperation(value = "新建设备", notes = "根据DDevice对象创建设备")
    @ApiImplicitParam(name = "DDevice", value = "设备实体DDevice", required = true, dataType = "DDevice")
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public ResponseEntity postDDevice(@RequestBody DDevice DDevice) {
        // 除了@ModelAttribute绑定参数之外，还可以通过@RequestParam从页面中传递参数
        if (mIDDeviceService.insert(DDevice)) {
            return new ResponseEntity(HttpStatus.CREATED);
        } else {
            return ResponseEntity.unprocessableEntity().build();
        }
    }


    @ApiOperation(value = "获取设备信息", notes = "根据url的SN来获取设备信息")
    @ApiImplicitParam(name = "sn", value = "设备SN", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "/{sn}", method = RequestMethod.GET)
    public ResponseEntity<DDevice> getDDevice(@PathVariable String sn) {
        EntityWrapper<DDevice> wrapper = new EntityWrapper<DDevice>();
        DDevice _mDDevice = new DDevice();
        _mDDevice.setSn(sn);
        wrapper.setEntity(_mDDevice);
        DDevice mDDevice = mIDDeviceService.selectOne(wrapper);
        if (mDDevice != null) {
            return ResponseEntity.ok(mDDevice);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @ApiOperation(value = "更新设备信息", notes = "根据url的SN来指定更新对象，并根据传过来的DDevice信息来更新设备信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sn", value = "设备SN", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "DDevice", value = "设备实体DDevice", required = true, dataType = "DDevice")
    })
    @RequestMapping(value = "/{sn}", method = RequestMethod.PUT)
    public ResponseEntity putDDevice(@PathVariable String sn, @RequestBody DDevice DDevice) {
        EntityWrapper<DDevice> wrapper = new EntityWrapper<DDevice>();
        DDevice.setSn(sn);
        wrapper.setEntity(DDevice);
        if (mIDDeviceService.update(DDevice, wrapper)) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.unprocessableEntity().build();
        }
    }


    @ApiOperation(value = "删除设备", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(name = "sn", value = "设备SN", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "/{sn}", method = RequestMethod.DELETE)
    public ResponseEntity deleteDDevice(@PathVariable String sn) {
        EntityWrapper<DDevice> wrapper = new EntityWrapper<DDevice>();
        DDevice mDDevice = new DDevice();
        mDDevice.setSn(sn);
        wrapper.setEntity(mDDevice);
        if (mIDDeviceService.delete(wrapper)) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}
